<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="_meta :: header">
    <title>编辑</title>
</head>
<body>
<!--不显示通过/驳回按钮-->
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form form form-horizontal" id="form-draft-edit" action="#"
              th:action="@{/draft/draftFinal/disallowUtil}" th:object="${draft}" enctype="multipart/form-data">
            <input type="hidden" name="id" th:value="${draft.id}"/>
            <input type="hidden" id="action" name="action" value="">

            <div class="layui-form-item">
                <label class="layui-form-label"><span
                        class="x-red">*</span>稿件标题：</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" th:value="${draft.draftTitle}" name="draftTitle"  placeholder="修改稿件标题" id="draftTitle" style="width:400px;"/>

                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">文件名：</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" th:value="${draft.fileName}" name="fileName"  placeholder="修改文件名" id="fileName" style="width:400px;"/>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><span
                        class="x-red">*</span>稿件内容：</label>
                <div class="layui-input-block">
                    <script id="editor" th:text="${draft.draftText}" name="draftText" autofocus type="text/plain" style="height: 500px">
                    </script>
                    <input id="editorInput" type="hidden" name="" th:value="${draft.draftText}">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><span
                        class="x-red">*</span>稿件类型：</label>
                <div class="layui-input-inline">
<!--                    <input  name="draftType" th:checked="${draft.draftType eq c.key}" type="radio"-->
<!--                            th:if="${ c.key >= 7}"-->
<!--                           th:each="c:${draftTypes}"  th:value="${c.key}" th:text="${c.value}"/>-->
                    <input type="hidden" th:value="${draft.draftType}" name="draftType">
                    <input type="text" class="layui-input" th:value="${draftTypes[draft.draftType]}" >
                    <!--<input type="text" class="layui-input" th:value="${draft.draftType}" name="draftType"  placeholder="修改稿件类型" id="draftType"/>-->
                </div>
            </div>
            <input type="hidden" name="draftNum"  placeholder="修改稿件字数" id="draftNum"/>

            <div class="layui-form-item" th:if="${checkFlag eq '1001'}">
                <label class="layui-form-label">驳回理由：</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入驳回理由" th:if="${reasonFlag ne '1001'}" type="text" rows="10"   cols="100"  id="disAllowdReason"  name="disAllowdReason"/></textarea>
                    <textarea placeholder="请输入驳回理由"  th:if="${reasonFlag eq '1001'}" th:text="${reason}" type="text" rows="10"   cols="100"  id="disAllowdReason"  name="disAllowdReason"/></textarea>
                </div>
            </div>

            <div class="layui-input-block">

            </div>
            <div class="layui-input-block" th:if="${buttonFlag eq '1001'}">
                <!--                <a title="驳回" href="javascript:;"-->
                <!--                   th:onclick="'javascript:disallowUtil(\'/draft/draftFinal/disallowUtil\',' + '\'' + ${draft.id} + '\')'"-->
                <!--                   class="layui-btn"  style="text-decoration:none">驳回</a>-->
                <a th:if="${agreeFlag ne '1001'}" title="通过" href="javascript:;" th:onclick="'pass(' + ${draft.id} + ')'"
                   class="layui-btn"  style="text-decoration:none">通过</a>
                <a  class="layui-btn" id="reject"   onclick="reject();" >
                    驳回
                </a>
                <a th:if="${updateFlag eq '1001'}" title="修改" href="javascript:;" th:onclick="'update(' + ${draft.id} + ')'"
                   class="layui-btn"  style="text-decoration:none">修改</a>
            </div>


        </form>
    </div>
</div>
<!--_footer 作为公共模版分离出去-->
<div th:replace="_footer :: footerjs"></div>
<div th:replace="_ueditor :: ueditor"></div>
<!--/_footer 作为公共模版分离出去-->
<!--请在下方写此页面业务相关的脚本-->
<script>
    layui.use(['form', 'layer','laydate'],function() {
        $ = layui.jquery;
        var form = layui.form,
            laydate = layui.laydate,
            layer = layui.layer;
        lay('.timeSpace').each(function(){
            laydate.render({
                elem: this
                ,trigger: 'click'
                ,type: 'datetime'
            });
        });
    });
    $(function () {
        $("#form-draft-edit").validate({
            rules: {//自定义规则，key:value 的形式，key 是要验证的元素，value 可以是字符串或对象。
                draftTitle:{
                    required: true,
                },
                draftText:{
                    required: true,
                },
                draftNum:{
                    required: true,
                },
                draftType:{
                    required: true,
                },
            },
            onkeyup:false,//类型 Boolean，默认 true，指定是否在敲击键盘时验证。
            debug : true,//进行调试模式（表单不提交）。
            success : "valid",//要验证的元素通过验证后的动作，如果跟一个字符串，会当作一个 css 类，也可跟一个函数。
            submitHandler : function (form) {//通过验证后运行的函数，里面要加上表单提交的函数($(form).ajaxSubmit();)，否则表单不会提交。
//            var data = UE.getEditor('editor').getContent();
//            $("#editorInput").val(data);
                var draftNum = UE.getEditor('editor').getContentTxt().length;
                $("#draftNum").val(draftNum);
                $.lock();
                $(form).ajaxSubmit({//使用 ajax 方式提交
                    type: 'POST',
                    url: "/draft/draftFinal/disallowUtil",
                    success: function (data) {
                        if (data.code == "1") {
                            selfmsg('驳回成功!', 1, function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.location.reload();
                                parent.layer.close(index);
                                $.unlock();
                            });
                        } else {
                            selfmsg('驳回失败!');
                            $.unlock();
                        }
                    },
                    error: function () {
                        selfmsg('驳回异常!', 5);
                        $.unlock();
                    }
                });
            }
        });
    });

    function showTxt()
    {
        var preHtml = UE.getEditor('editor').getContent(); <!-- execCommand( 'preview' );-->
        layer.open({
            title: '稿件预览',
            content: preHtml,
            btn: ['确定', '取消'],
            yes: function(index, layero){//两个参数:当前层索引,当前层DOM对象
                //按钮【确认】的回调
            },
            btn2: function(index, layero){
                //按钮【取消】的回调
                //return false 开启该代码可禁止点击该按钮关闭
            },
        });
    };
</script>
<!--/请在上方写此页面业务相关的脚本-->

<script>
    function reject(){
        if($('#disAllowdReason').val().trim().length == 0){
            selfmsg('请输入驳回理由!');
            return;
        }

        $('#reject').submit();
    }

    layui.use(['laydate','form'], function(){
        var laydate = layui.laydate;
        var form = layui.form;
        // 监听全选
        form.on('checkbox(checkall)', function(data){

            if(data.elem.checked){
                $('tbody input').prop('checked',true);
            }else{
                $('tbody input').prop('checked',false);
            }
            form.render('checkbox');
        });
        form.on('select(selectchange)', function(data){
            console.log(data.value); //得到被选中的值
            var pageNo = $('#pageNo');
            var Form = $('form')
            pageNo.val(1);
            Form.submit();
            form.render('select');
        });
        //日期时间范围选择
        laydate.render({

        });
        lay('.timeSpace').each(function(){
            laydate.render({
                elem: this
                ,type: 'datetime'
                ,range: true //或 range: '~' 来自定义分割字符
            });
        });
    });
    function pass(draftId) {
        var draftText = UE.getEditor('editor').getContent();
        var draftTitle = $('#draftTitle').val();
        var fileName = $('#fileName').val();
        var draftNum = UE.getEditor('editor').getContentTxt().length;
        layer.open({
            title: '审核提示',
            content: '确认审核通过吗?',
            btn: ['确认', '取消'],
            yes: function(index, layero){//两个参数:当前层索引,当前层DOM对象
                //按钮【确认】的回调
                $.lock()
                $.ajax({
                    type:'post',
                    url:"/draft/partner/check",
                    cache: false,
                    //重点必须为一个变量如：data
                    data:{"draftId":draftId,
                        "draftText":draftText,
                        "draftTitle":draftTitle,
                        "fileName":fileName,
                        "draftNum":draftNum
                    },
                    dataType:'json',
                    success:function(data){
                        if (data.code == "1") {
                            selfmsg('审核通过!', 1, function () {
                                window.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.location.reload();
                                parent.layer.close(index);
                                $.unlock()
                            });
                        }else {
                            selfmsg('提交失败!');
                            $.unlock()
                        }
                    },
                    error:function(){
                        selfmsg('提交异常!',5);
                        $.unlock()
                    }
                });
            },
            btn2: function(index, layero){
                //按钮【取消】的回调
                //return false 开启该代码可禁止点击该按钮关闭
            },
        });
    };

    function update(draftId) {
        var draftText = UE.getEditor('editor').getContent();
        var draftTitle = $('#draftTitle').val();
        var fileName = $('#fileName').val();
        var draftNum = UE.getEditor('editor').getContentTxt().length;
        layer.open({
            title: '修改提示',
            content: '确认修改吗?',
            btn: ['确认', '取消'],
            yes: function(index, layero){//两个参数:当前层索引,当前层DOM对象
                //按钮【确认】的回调
                $.lock()
                $.ajax({
                    type:'post',
                    url:"/draft/partner/edit",
                    cache: false,
                    //重点必须为一个变量如：data
                    data:{"draftId":draftId,
                        "draftText":draftText,
                        "draftTitle":draftTitle,
                        "fileName":fileName,
                        "draftNum":draftNum
                    },
                    dataType:'json',
                    success:function(data){
                        if (data.code == "1") {
                            selfmsg('修改成功!', 1, function () {
                                window.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                //parent.location.reload();
                                // parent.layer.close(index);
                                $.unlock()
                            });
                        }else {
                            selfmsg('修改失败!');
                            $.unlock()
                        }
                    },
                    error:function(){
                        selfmsg('修改异常!',5);
                        $.unlock()
                    }
                });
            },
            btn2: function(index, layero){
                //按钮【取消】的回调
                //return false 开启该代码可禁止点击该按钮关闭
            },
        });
    };

    function disallowUtil(draftId,disAllowdReason) {
        layer.open({
            title: '审核提示',
            content: '确认驳回稿件吗?',
            btn: ['确认', '取消'],
            yes: function(index, layero){//两个参数:当前层索引,当前层DOM对象
                //按钮【确认】的回调
                $.lock()
                $.ajax({
                    type:'post',
                    url:"/draft/draftFinal/disallowUtil",
                    cache: false,
                    data:{"draftId":draftId,"disAllowdReason":disAllowdReason},  //重点必须为一个变量如：data
                    dataType:'json',
                    success:function(data){
                        if (data.code == "1") {
                            selfmsg('已驳回!', 1, function () {
                                window.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.location.reload();
                                parent.layer.close(index);
                                $.unlock()
                            });
                        }else {
                            $.unlock()
                            selfmsg('驳回失败!');
                        }
                    },
                    error:function(){
                        $.unlock()
                        selfmsg('提交异常!',5);
                    }
                });
            },
            btn2: function(index, layero){
                //按钮【取消】的回调
                //return false 开启该代码可禁止点击该按钮关闭
            },
        });
    };
    function submitFun(act)
    {
        $('#action').val(act);
    };


</script>
</body>
</html>